/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.path;

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

import scenic.Util;

/**
 * This class defines a cubic Bezier curve. This class does not 
 * define the start point of the curve. The start point is the 
 * end point of the previous segment.
 */
public class CubicBezierSegment extends CurvedSegment {
	private double x1, y1;
	private double x2, y2;
	private double x3, y3;
	
	/**
	 * Creates a cubic bezier segment with the given parameters. The
	 * first control point is not defined here, it is defined by the end
	 * point of the previous segment.
	 * 
	 * @param x1 x coordinate of second control point.
	 * @param y1 y coordinate of second control point.
	 * @param x2 x coordinate of third control point.
	 * @param y2 y coordinate of third control point.
	 * @param x3 x coordinate of fourth control point.
	 * @param y3 y coordinate of fourth control point.
	 */
	public CubicBezierSegment(double x1, double y1, 
			double x2, double y2, 
			double x3, double y3) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.x3 = x3;
		this.y3 = y3;
	}
	
	/**
	 * Gets the x coordinate of second control point.
	 */
	public double getX1() {
		return x1;
	}

	/**
	 * Gets the y coordinate of second control point.
	 */
	public double getY1() {
		return y1;
	}

	/**
	 * Gets the x coordinate of third control point.
	 */
	public double getX2() {
		return x2;
	}

	/**
	 * Gets the y coordinate of third control point.
	 */
	public double getY2() {
		return y2;
	}

	/**
	 * Gets the x coordinate of fourth control point.
	 */
	public double getX3() {
		return x3;
	}

	/**
	 * Gets the y coordinate of fourth control point.
	 */
	public double getY3() {
		return y3;
	}

	public Point2D.Double calcPoint(double t, Point2D.Double position) {
		double it = 1.0 - t;
		double a1 = it * it * it;
		double a2 = 3.0 * t * it * it;
		double a3 = 3.0 * t * t * it;
		double a4 = t * t * t;
		Point2D.Double point = new Point2D.Double();
		
		point.x = a1 * position.getX() + a2 * x1 + a3 * x2 + a4 * x3;
		point.y = a1 * position.getY() + a2 * y1 + a3 * y2 + a4 * y3;
		
		return point;
	}

	public void getBounds(AffineTransform transform, Rectangle r, Point2D.Double position) {
		Util.addPointToBounds(transform, r, x1, y1);
		Util.addPointToBounds(transform, r, x2, y2);
		Util.addPointToBounds(transform, r, x3, y3);
		position.x = x3;
		position.y = y3;	
	}
}
